from flask import Blueprint,request,jsonify
from utils.db import db




import json



bp_work = Blueprint('workl', __name__)

@bp_work.route('/add/',methods=['POST',"GET","DELETE"])
def add():
    #添加工作流
    if request.method =="POST":
        #判断数据是什么传播过来的 然后怎么接收
        data = request.data
        if data:
            data = json.loads(request.data)
        else:
            data = request.form
        sql = "insert into workflow(`id`,`name`) values(%d,'%s') " % (0,data['name'])
        db.insert(sql)

        return jsonify({'code':200})

    #获取工作流进行展示
    elif request.method =="GET":
        id =request.args.get('id')
        if id:
            sql = "select * from workflow where id=%d"%(int(id))
            #获取符合id的信息
            res = db.find_one(sql)
            print(res)
            #retur 返回的只有字符串  不能返回结构 所以用jsonify
            return jsonify({'code':200,'list':res['params']})
        else:
            sql = "select * from workflow"
            #获取全部信息
            res = db.find_all(sql)

            #retur 返回的只有字符串  不能返回结构 所以用jsonify
            return jsonify({'code':200,'list':res})
    else:
        pass


#参数添加到数据库
@bp_work.route('/addwork/',methods=["POST","GET","DELETE"])
def addwork():
    if request.method =="POST":
        data = request.data
        if data:
            data = json.loads(request.data)
        else:
            data = request.form
        print(data)
        
        sql = "update workflow set params='%s' where id=%d "%(data['params'],int(data['id']))
        # db.update(sql)
        return 'ok'


    elif request.method =="GET":
        pass
    else:
        pass

#添加记录
@bp_work.route('/record/',methods=["POST","GET","DELETE"])
def record():
    if request.method =="POST":
        data = request.data
        if data:
            data = json.loads(request.data)
        else:
            data = request.form
        print(data)
        sql = "insert into record(`wid`,`params`) values(%d,'%s')"%(int(data['wid']),data['params'])
        db.insert(sql)
        return 'ok'

    elif request.method =="GET":
        pass
    else:
        pass




def xtree(data):
    if data:
        tree = {}
        list = []
        #先抓换成字典
        for i in data:
            tree[i['id']] = i
        for j in data:
            if j['pid'] == 0:
                list.append(j)
            else:
                if 'son' not in tree[j['pid']]:
                    tree[j['pid']]['son'] = []
                tree[j['pid']]['son'].append(j)
        
        return list




#无限级分类
@bp_work.route('/cates/',methods=["POST","GET","DELETE"])
def cates():
    if request.method =="POST":
        pass

    elif request.method =="GET":
        #获取全部数据
        sql = "select * from cates"
        res = db.find_all(sql)
        print(res)
        
        list = xtree(res)
        return jsonify({'code':200,'list':list})
    else:
        pass
